/*
 * Copyright (c) 2011 CCXE(China) Co.,Ltd. All Rights Reserved.
 *
 * This software is the confidential and proprietary information of
 * CCXE(China) Co.,Ltd. ("Confidential Information").
 * It may not be copied or reproduced in any manner without the express 
 * written permission of CCXE(China) Co.,Ltd.
 *
 * Author: zhaosj
 * Date: 2011-9-7 下午18:15:21
 */
package com.swing.excelplugins.panel;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.EventQueue;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.MouseAdapter;
import java.awt.event.MouseEvent;
import java.io.File;
import java.io.Serializable;

import javax.swing.JButton;
import javax.swing.JDialog;
import javax.swing.JFileChooser;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTextField;

import com.swing.excelplugins.listener.TradePanelListener;

/**
 * EXCEL PLUGINS WSDL GENERATOR LISTENER
 * 
 * @author zhaosj
 * 
 */

public class MainPanel extends JDialog implements Serializable {

	private static final long serialVersionUID = -2842545262616522390L;

	private MainPanel self = this;

	private JLabel labelIpAndPort = new JLabel("ip : port");

	private JTextField fieldIp = new JTextField();
	private JTextField fieldPort = new JTextField();
	private JTextField fieldFilePath = new JTextField();

	FolderPanel labelFilePath = new FolderPanel();

	private JButton subBtn = new JButton("SUBMIT");
	private JButton cnlBtn = new JButton("EXIT");

	private TradePanelListener lis = new TradePanelListener();

	public MainPanel() {
		initProperty();
		initPosition();
		addComponents();
		initEventListener();
	}

	/**
	 * 设置面板属性
	 */
	private void initProperty() {
		try {

			this.setLayout(null);
			this.setAlwaysOnTop(true);
			this.setModal(true);

		} catch (Exception e) {

			e.printStackTrace();
		}
	}

	/**
	 * 初始化监听事件
	 */
	private void initEventListener() {
		cnlBtn.addMouseListener(new MouseAdapter() {
			@Override
			public void mouseClicked(MouseEvent e) {
				if (e.getButton() == 1) {
					self.dispose();
				}
			}
		});

		lis.setPanel(self);
		subBtn.addMouseListener(lis);
	}

	/**
	 * 添加组件
	 */
	private void addComponents() {

		add(labelIpAndPort);
		add(labelFilePath);

		add(fieldIp);
		add(fieldPort);
		add(fieldFilePath);

		add(subBtn);
		add(cnlBtn);

	}

	/**
	 * 初始化面板位置和组件位置
	 */
	private void initPosition() {
		initSelfPosition();
		initComponentsPosition();
	}

	/**
	 * 初始化各组件位置、初始值
	 */
	private void initComponentsPosition() {
		int labelWidth = 60; // label宽度
		int fieldWidth = 200; // 文本框宽度
		int height = 25; // 组件高度
		int rowledge = 40; // 行距

		// test label
		fieldIp.setText("10.0.2.215");
		fieldPort.setText("80");

		labelIpAndPort.setBounds(20, 20, labelWidth, height);
		fieldIp.setBounds(labelIpAndPort.getX() + labelIpAndPort.getWidth(), labelIpAndPort.getY(), fieldWidth - 60, height);
		fieldPort.setBounds(fieldIp.getX() + fieldIp.getWidth() + 10, fieldIp.getY(), 50, height);

		labelFilePath.setBounds(5, labelIpAndPort.getY() + rowledge, 320, height + 10);

		subBtn.setBounds(60, labelFilePath.getY() + rowledge + 10, 100, height + 20);
		cnlBtn.setBounds(subBtn.getX() + subBtn.getWidth() + 10, subBtn.getY(), 100, height + 20);

	}

	/**
	 * 设定主程序窗口的位置
	 */
	private void initSelfPosition() {

		int panelWidth = 350;
		int panelHeight = 200;

		Dimension screenD = this.getToolkit().getScreenSize();
		int width = (int) screenD.getWidth();
		int height = (int) screenD.getHeight();
		setSize(panelWidth, panelHeight);
		setTitle("EXCEL PLUGINS WSDL GENERATOR");
		setLocation((width - panelWidth) / 2, (height - panelHeight) / 2);
		setResizable(false);
	}

	public JTextField getFieldPort() {
		return fieldPort;
	}

	public void setFieldPort(JTextField fieldPort) {
		this.fieldPort = fieldPort;
	}

	public JTextField getFieldFilePath() {
		return fieldFilePath;
	}

	public void setFieldFilePath(JTextField fieldFilePath) {
		this.fieldFilePath = fieldFilePath;
	}
	
	public JTextField getFieldIp() {
		return fieldIp;
	}

	public void setFieldIp(JTextField fieldIp) {
		this.fieldIp = fieldIp;
	}

	// 选择文件夹
	private class FolderPanel extends JPanel {

		private static final long serialVersionUID = -6255197088636849030L;

		JLabel label = new JLabel("file path");
		JTextField fileText = new JTextField(15);
		JButton chooseButton = new JButton("VIEW");

		FolderPanel() {

			chooseButton.setSize(40, 20);
			fileText.setEditable(false);

			this.add(label);
			this.add(fileText);
			this.add(chooseButton);
			//this.setBackground(Color.gray);
			clickAction ca = new clickAction(this);
			chooseButton.addActionListener(ca);

		}

		public String getFileName() {
			JTextField jtf = (JTextField) this.getComponent(1);
			return jtf.getText();
		}

		private class clickAction implements ActionListener {
			clickAction(Component c) {
				cmpt = c;
			}

			public void actionPerformed(ActionEvent event) {
				JFileChooser chooser = new JFileChooser();
				chooser.setFileSelectionMode(JFileChooser.DIRECTORIES_ONLY); // 只显示目录
				chooser.setCurrentDirectory(new File("."));
				int ret = chooser.showOpenDialog(cmpt);
				if (ret == JFileChooser.APPROVE_OPTION) {
					JPanel jp = (JPanel) cmpt;
					JTextField jtf = (JTextField) jp.getComponent(1);
					jtf.setText(chooser.getSelectedFile().getPath());
					fieldFilePath.setText(chooser.getSelectedFile().getPath());
				}
			}

			private Component cmpt;
		}
	}

	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			@Override
			public void run() {
				MainPanel d = new MainPanel();
				d.setVisible(true);
			}
		});
	}
}
